package com.project.controller;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service;
    @RequestMapping("login")
    public String login(String userName, String pwd, HttpServletRequest req){
        UserBean user=service.login(userName, pwd);
        if (user == null){
            return "no";
        }
        else {
            req.getSession().setAttribute("loginUser",user);
            return "ok";
        }
    }
    @RequestMapping("getLoginUser")
    public UserBean getLoginUser(HttpServletRequest request){
        return (UserBean) request.getSession().getAttribute("loginUser");
    }
    @RequestMapping("destorySession")
    public String destorySession(HttpServletRequest request){
        request.getSession().invalidate();
        return "ok";
    }
    @RequestMapping("add")
    public String add(UserBean user){
        UserBean user1 = service.findByName(user.getName());
        if (user1 == null){
            service.add(user);
            return "ok";
        }
        return "no";
    }
}
